/*
 * Copyright (c) 2005, 2020, Jin Lin All rights reserved.
 * Jin Lin PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 */
package aco.module.iteration;

import aco.entity.dobject.*;
import aco.entity.dtobject.BaseDataDTO;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述 蚁群算法.
 *
 * @author Jin Lin
 * @created 2019 -06-30 14:49:44
 */
public class AcaSearchHandler extends AbstractSearchHandle {

    /**
     * 描述 初始化基本数据.
     *
     * @return the base data dto
     * @author Jin Lin
     * @created 2019 -06-30 14:46:07 Init base data.
     */
    @Override
    BaseDataDTO initBaseData() {
        Long taskLength = 100L;
        Long nodeLength = 10L;
        Long min = 10L;
        Long max = 100L;

        Efficiency efficiency = new Efficiency();
        efficiency.initRandomNodes(nodeLength, min, max);
        Cost cost = new Cost();
        cost.initRandomTasks(taskLength, min, max, efficiency.getNodes());
        return new BaseDataDTO(efficiency, cost);
    }

    /**
     * 描述 初始化算法参数.
     *
     * @return the ant
     * @author Jin Lin
     * @created 2019 -06-30 14:46:07 Init task info and pheromone.
     */
    @Override
    Trainer initTaskInfoAndPheromone(Cost cost) {
        //迭代次数（蚁群数量）
        Long colonyNum = 10L;
        //每个蚁群包含的蚂蚁数量
        Long antNum = 10L;
        //变异蚂蚁编号最小值
        Long minCriticalPoint = 3L;
        //变异蚂蚁编号最大值
        Long maxCriticalPoint = 7L;
        //信息素增加比例
        Double increaseRatio = 2D;
        //信息素衰减比例
        Double attenuationRatio = 0.5D;
        //初始化蚁群
        Trainer trainer = new Trainer();
        List<Colony> colonies = new ArrayList<>();
        //一个蚁群就是一次迭代
        for (Long i = 0L; i < colonyNum; i++) {
            Colony colony = new Colony();
            //使用随机值设置变异蚂蚁的临界编号(临界编号以上的蚂蚁，不根据信息素浓度选择策略)
            colony.setCriticalPoint(
                    new Double(
                            Math.random() * (maxCriticalPoint - minCriticalPoint) + minCriticalPoint
                    ).longValue()
            );
            colony.initAnts(antNum, increaseRatio, attenuationRatio, cost);
            colonies.add(colony);
        }
        trainer.setColonies(colonies);
        return trainer;
    }

    /**
     * 描述 搜索迭代.
     *
     * @param baseDataDTO the base data dto
     * @param trainer     the trainer
     * @author Jin Lin
     * @created 2019 -07-01 22:11:15 Search.
     */
    @Override
    void search(BaseDataDTO baseDataDTO, Trainer trainer) {
        List<Colony> colonies = trainer.getColonies();
        //1.每一个蚁群的搜索
        for (long i = 0L; i < trainer.lengthOfColonies(); i++) {
            Colony colony = colonies.get((int)i);
            List<Ant> ants = colony.getAnts();
            //每一只蚂蚁的搜索
            for (Long j = 0L; j < trainer.lengthOfColonies(); j++) {
                Ant ant = ants.get(Integer.parseInt(j + ""));


            }

        }
    }

}
